import '../helper/Helper.dart';
import '../helper/dioHelper.dart';

class User {
  int Id;
  String Guid;
  String Name;
  String UserName;
  String PassWord;
  String Phone;
  String? Mail;
  String Authority;
  String Position;
  String Department;
  String? DingDingID;
  String? JPushID;

  User(
      this.Id,
      this.Guid,
      this.Name,
      this.UserName,
      this.PassWord,
      this.Phone,
      this.Mail,
      this.Authority,
      this.Position,
      this.Department,
      this.DingDingID,
      this.JPushID);
  User.fromJson(Map<String, dynamic> json)
      : Id = json["Id"],
        Guid = json["Guid"],
        Name = json["Name"],
        UserName = json["UserName"],
        PassWord = json["PassWord"],
        Phone = json["Phone"],
        Mail = json["Mail"],
        Authority = json["Authority"],
        Position = json["Position"],
        Department = Helper.toStr(json["Department"]),
        DingDingID = json["DingDingID"],
        JPushID = json["JPushID"];
  Map<String, dynamic> toJson() => {
        'Id': Id,
        'Guid': Guid,
        'Name': Name,
        'UserName': UserName,
        'PassWord': PassWord,
        'Phone': Phone,
        'Mail': Mail,
        'Authority': Authority,
        'Position': Position,
        'Department': Department,
        'DingDingID': DingDingID,
        'JPushID': JPushID,
      };
  static Future<bool> Add(User x1) async {
    var response = await DioHelper.Post("/User/Add", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Update(User x1) async {
    var response = await DioHelper.Post("/User/Update", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Delete(User x1) async {
    var st = x1.toJson();
    var response = await DioHelper.Post("/User/Delete", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<User?> Get(String where, dynamic data) async {
    var response = await DioHelper.Post("/User/GetByWhere", {
      "Where": where,
      "obj": data,
    });

    if (response.statusCode == 200) return new User.fromJson(response.data);
    return null;
  }

  static Future<List<User>> GetList(
      String where, dynamic data, int pageIndex, int pageSize) async {
    var response = await DioHelper.Post("/User/GetListByWhere", {
      "Where": where,
      "obj": data,
      "PageIndex": pageIndex,
      "PageSize": pageSize,
    });
    var list1 = <User>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(new User.fromJson(x));
      }
    }
    return list1;
  }

  static Future<List<User>> GetAllList() async {
    var response = await DioHelper.Get("/User/GetList", {});
    var list1 = <User>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(new User.fromJson(x));
      }
    }
    return list1;
  }
}
